import Api from '../../constants/Api'

export default {
  namespaced: true,
  state: {
    menuDataList: [],
    allMenuDataList: []
  },
  mutations: {
    detailListTo(state, data) {
      state.menuDataList = data
    },
    allMenuListTo(state, data) {
      state.allMenuDataList = data
    }
  },
  actions: {
    getMenuList({commit}, that) {
      that.$axios({
        method: 'get',
        url: Api.getMenuList
      }).then(res => {
        commit('detailListTo', res.data)
        that.tableLoading = false
      }).catch(() => {
        that.tableLoading = false
      })
    },
    getAllMenuList({commit}, that) {
      that.$axios({
        method: 'get',
        url: Api.getAllMenuList
      }).then(res => {
        commit('allMenuListTo', res.data)
        that.tableLoading = false
      }).catch(() => {
        that.tableLoading = false
      })
    },
    addSysMenu({commit}, that) {
      that.$axios({
        method: 'post',
        url: Api.addSysMenu,
        data: that.sysMenuForm
      }).then(res => {
        if (res.code === 20002) {
          that.handleVisible = false
          that.$message.success(res.msg)
          that.getData()
        } else {
          that.$message.error(res.msg)
        }
      }).catch(error => {
        that.$message.error(error.data.message)
      })
    },
    editSysMenu({state}, that) {
      that.$axios({
        method: 'post',
        url: Api.editSysMenu,
        data: that.sysMenuForm
      }).then(res => {
        if (res.code === 20003) {
          that.handleVisible = false
          that.$message.success(res.msg)
        } else {
          that.$message.error(res.msg)
        }
        that.getData()
      }).catch(error => {
        that.$message.error(error.data.message)
      })
    },
    removeMenu({state}, that) {
      let url = Api.removeMenu.replace('{menuId}', that.sysMenuForm.menuId)
      return that.$axios({
        method: 'delete',
        url: url
      }).then(res => {
        if (res.code === 20004) {
          that.delVisible = false
          that.$message.success(res.msg)
          that.getData()
        } else {
          that.$message.error(res.msg)
        }
      }).catch(error => {
        that.$message.error(error.data.message)
      })
    },
    getPerById({state}, that) {
      that.$axios({
        method: 'get',
        url: Api.getPermissionById,
        params: {menuId: that.sysPermissionForm.menuId}
      }).then(res => {
        if (res.code === 20001) {
          if (res.data !== null && res.data.length > 0) {
            that.handleTitleP = '编辑权限'
            that.sysPermissionForm = {
              parentId: res.data[0].parentId,
              perId: res.data[0].perId,
              perName: res.data[0].perName,
              perStatus: parseInt(res.data[0].perStatus),
              menuId: res.data[0].menuId,
              menuName: res.data[0].menuName,
              menuType: parseInt(res.data[0].menuType)
            }
          } else {
            that.handleTitleP = '添加权限'
            that.sysPermissionForm.perStatus = 0
            that.sysPermissionForm.perId = null
          }
          that.handleVisibleP = true
        } else {
          that.$message.error(res.msg)
        }
      }).catch(error => {
        that.$message.error(error)
      })
    },
    addSysPermission({commit}, that) {
      that.$axios({
        method: 'post',
        url: Api.addSysPermission,
        data: that.sysPermissionForm
      }).then(res => {
        if (res.code === 20002) {
          that.$message.success(res.msg)
        } else {
          that.$message.error(res.msg)
        }
      })
    },
    editSysPermission({commit}, that) {
      that.$axios({
        method: 'post',
        url: Api.editSysPermission,
        data: that.sysPermissionForm
      }).then(res => {
        if (res.code === 20003) {
          that.$message.success(res.msg)
        } else {
          that.$message.error(res.msg)
        }
      })
    }
  }
}
